package Messages;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.math3.stat.Frequency;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class implements leates report from db
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageLatesReport {
	private ConnectionToClient client ;
	int lates,count;
	private double wantedavg,wantedemid;
	Frequency wantedfreq,freq;
	/*
	 * constractor of this class 
	 */
	public MessageLatesReport(ConnectionToClient client) {
		super();
        this.client = client;
	}
/**
 * generate report from DB 41997
 * 
 * @param str
 */
	@SuppressWarnings("deprecation")
	public void Report(String[] str) {
		
		
            
            try 
            {
                    Statement stmt;
                    SQLConnection conn = new  SQLConnection();
                    stmt = conn.getConnection().createStatement();
                    
                    
                                    
                                    
                    ResultSet rs1=stmt.executeQuery("SELECT * FROM db1.Lending  WHERE EndDate<ActualReturnDate AND ActualReturnDate >='"+str[1]+"' AND ActualReturnDate<='"+str[2]+"'AND ActualReturnDate<>'"+null+"';");
                  
                    ArrayList<Object> arr=new ArrayList<Object>();
                    ArrayList<Integer> arr2=new ArrayList<Integer>();                
                                    
                            try {
                                    
                                            count=0;
                                            if (rs1.next())
                                                    {
                                            		 rs1.previous();
                                                     while (rs1.next())
                                                     {	
                                                    	 lates=rs1.getDate(6).getDate()-rs1.getDate(5).getDate();
                                                    	 count=count+1;
                                                    	 arr2.add(lates);
                                                     }
                                                   }
                                            wantedavg=calcAvarage(arr2);
                            				wantedemid=calcMedian(arr2);
                            				String average = Double.toString(wantedavg);
                            				String median = Double.toString(wantedemid);
                            				arr.add("LatesReport");
                            				arr.add(average);
                            				arr.add(median);
                            				arr.add(creatfreq(arr2).get(0));
                            				arr.add(creatfreq(arr2).get(1));
                            				client.sendToClient(arr);
                                    }
                                            catch (SQLException e)
                                                            {       
                                                            e.printStackTrace();
                                                            }
            					}

            					catch (Exception ex )
            					{
            						ex.printStackTrace();
            					}
            
   
	

}
	protected double calcAvarage(ArrayList<Integer> list){
		DescriptiveStatistics stat = new DescriptiveStatistics();
		for (Integer x : list){
			stat.addValue(x);
		}
		return stat.getMean();

	}
	protected double calcMedian(ArrayList<Integer> a){		
		Integer[] list = new Integer[a.size()];
	    a.toArray(list);
	    Arrays.sort(list);
	    Collections.sort(a);
	    if (a.size() >0){
	    	if((a.size() % 2) == 0) 
	    		return ( ( a.get(a.size()/2) + a.get(a.size()/2-1) ) / 2);         
	    	return ( a.get(a.size()/2) );   
	    }
	    return 0;
	}
	public ArrayList<Object> creatfreq(ArrayList<Integer> countwnated){
		Map<Double, Double> map = new HashMap<Double, Double>();
		Double val11;
		Collections.sort(countwnated);
		int max1,min1,size,j;
		size=countwnated.size()-1;
		max1=countwnated.get(size);
		min1=countwnated.get(0);
		Double max,min;
		Double prec22;
		 max=(double)max1;
		 min=(double)min1;
		
		j=0;
		Double r=(double) ((max-min)/10);
		
		
		if(r==0)
			map.put(max,1.0);
			else{	
		for(double i=min+r;i<=max;i=r+i){
			int count =0;
			while((countwnated.get(j))<=i){
				count=count+1;
				
				if(j==size)
					break;
				j=j+1;
			}
			prec22=(double)count/size;
			val11 = (double) i; 
			map.put(val11, prec22);
			
		}
		
		
		
		
	}
		ArrayList<Object> Arr=new ArrayList<Object>();
		Arr.add(map);
		Arr.add(r);
		return Arr;
	
	
	}
	
}
